/*
 * 自己的JS脚步
 * @Author: yanxu
 * @Date:   2016-09-08 10:22:26
 */

'use strict';

/*$("input").bind({
    "focus": function () {
        if(value==defaultValue){value='';this.style.color='#000'}
    },
    "blur": function () {
        if(!value){value=defaultValue;this.style.color='#999'}
    }
});*/
var a = null;

//给所有的button添加点击效果
$("button").bind({
    "touchstart": function () {
      a =  $(this).css('background-color');
        this.style.backgroundColor="#ccc";
    },

    "touchend": function () {

        this.style.backgroundColor=a;
    }
});

//给所有的button添加点击效果  结束


//让高度等于宽度  开始
var w =$(".height-equals-width").css("width");
$(".height-equals-width").css("height",w);

//让高度等于宽度  结束


//获取url里边的值
// function getQueryString(name)
// {
//     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)", "i");
//     var r = window.location.search.substr(1).match(reg);
//     if(r!=null)return  unescape(r[2]);
//     return null;
// }



//增加流量统计代码
//友盟统计

var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1260836612'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1260836612' type='text/javascript'%3E%3C/script%3E"));

$('#cnzz_stat_icon_1260836612').css({
	'display':'none'
})


//百度统计

var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?65216f44f08f1e9d8f64c5adb24fb698";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();


// 尾部点击事件
$('#footer-nav  div').eq(2).on('click', function () {
    $('.markdown').show().siblings().addClass('blur');
})
$('.releaseMessage img').on('click', function () {
    var href = $(this).data('href');
    if(href){
        window.location.href = href;
    }else{
        $('.markdown').hide().siblings().removeClass('blur');
    }
})

// 公共搜索

function searchPublish() {
    var way = 0,
        categoryApi = '/category/getMeatCategoryInfos',
        // 热门搜索
        // hotS = function () {
        // var Api;
        //     if(way){
        //         Api = '';
        //     }else {
        //         Api = '';
        //     }
        //     $.get()
        // },
        history = function () {
            $('#search-main .search-history').empty();
            var seachHistory = JSON.parse(window.localStorage.getItem('keyWord' + way)) || [], str = '';
            if(seachHistory.length){
                $.each(seachHistory, function () {
                    str += '<li>'+this+'</li>'
                })
            }else {
                str = '<li>无</li>'
            }
            $('#search-main .search-history').append(str)
        },
        goNewPage = function (data) {
            if(data === '无'){
                return;
            }
            var seachHistory = JSON.parse(window.localStorage.getItem('keyWord' + way)) || [];
            var index = seachHistory.indexOf(data);
            var meatType = $('#search-header input[type="hidden"]').val();
            if(index >= 0 ){
                seachHistory.splice(index, 1)
            }
            seachHistory.unshift(data)
            if(seachHistory.length>5){
                seachHistory.length = 5;
            }
            window.localStorage.setItem('keyWord' + way,JSON.stringify(seachHistory))
            if(way === 0){
                window.location.href = 'supply.html?keyWord='+data +'&meatType='+meatType;
            }else if(way === 1){
                window.location.href = 'orderBuy.html?keyWord='+data +'&meatType='+meatType;
            }else {
                window.location.href = 'supermarket.html?shopName='+data;
            }
        };
    $('#search input').blur();
    var pageii = layer.open({
        type: 1
        ,content:"<header id='search-header'><div class='back'><img src='img/icon_right.png'></div><div class='searchI'><form action='javascript:;' id='formFor'><input type='hidden' name='meatype' value='0'><input type='search' name='keyWord' placeholder='请输入搜索关键词'><div class='option'><span>牛肉</span> <img src='img/shop/qiehuan.png'></div><div class='searchRe'></div></form><ul class='searchResoult hidden'></ul></div></header><main id='search-main'><ul class='title'><li data-way='0' class='search-active'><span><i></i>供应</span></li><li data-way='1'><span><i></i>采购</span></li><li class='hidden' data-way='2'><span><i></i>店铺</span></li></ul><h3>历史搜索 <span></span></h3><ul class='search-history'></ul></main>"
        ,anim: 'up'
        ,style: 'position:fixed; left:0; top:0; background-color:#F5F5F5; width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;'
        ,success: function (layero, index) {
            var arr = ['牛肉', '羊肉']
            // 默认渲染 历史记录
            history()
            // 搜索点击
            $('#search-header .option').on('click', function () {
                var text = $(this).find('span').text();
                if(text === '牛肉'){
                    $(this).find('span').text('羊肉')
                    $('#search-header input[type="hidden"]').val('1')
                }else {
                    $('#search-header input[type="hidden"]').val('0')
                    $(this).find('span').text('牛肉')
                }
            })
            // 模糊搜索提示
            $('#search-header input[type="search"]').on('input', function () {
                var val = $(this).val().trim(),
                    _this = $(this);
                if(val){
                    $.getJSON(host+categoryApi,{keyWord:val}, function (data) {
                        if(data.success){
                            var numData = data.data.slice(0, 10),htmlStr = '';
                            $.each(numData, function () {
                                htmlStr += '<li>'+this.meatName+'</li>'
                            })
                            _this.parent('form').siblings('.searchResoult').removeClass('hidden').html(htmlStr)
                        }else {
                            console.log(data.message)
                        }
                    })
                }else {
                    _this.parent('form').siblings('.searchResoult').addClass('hidden')
                }
            })

            // 模糊搜索结果点击
            $('#search-header .searchResoult').on('click', 'li', function () {
                var value = $(this).text().trim()
                if(!value){
                    return
                }
                goNewPage(value)
            })
            $('#formFor').on('submit',function () {
                var vla = (document.forms['formFor'][1].value).trim();
                if(vla){
                    goNewPage(vla)
                }
            })
            // 清除历史纪录
            $('#search-main h3 span').on('click', function () {
                window.localStorage.removeItem('keyWord' + way)
                $('#search-main .search-history').empty();
                $('#search-main .search-history').append('<li>无</li>')
            })
            // 搜索按钮点击
            $('#search-header .searchRe').on('click', function () {
                var value = $('#search-header input[type="search"]').val().trim();
                if(!value){
                    return
                }
                goNewPage(value)
            })
            // 历史搜索点击
            $('#search-main .search-history').on('click', 'li', function () {
                var value = $(this).text().trim()
                if(!value){
                    return
                }
                goNewPage(value)
            })

            // 切换teb lan
            $('#search-main .title li').on('click', function () {
                var val = $(this).data('way')
                if(val != undefined) {
                    $(this).addClass('search-active').siblings('li').removeClass('search-active')
                    way = val;
                    history();
                    if(way === 1){
                        $('.option').show()
                        $('#formFor input[type="search"]').attr('name', 'keyWord')
                    }else if(way === 0){
                        $('.option').show()
                        $('#formFor input[type="search"]').attr('name', 'keyWord')
                    }else {
                        $('.option').hide()
                        $('#formFor input[type="search"]').attr('name', 'shopName')
                    }

                }

            })
            // // 返回点击
            $('#search-header .back').on('click', function () {
                layer.closeAll();
            })
        }
    });
}

// 时间
//更新时间的
function time(strTime){

    var oldTime = (new Date(strTime)).getTime();   //情求得更新时间
    var date2=new Date();    //当前时间
    var date3= date2.getTime() -oldTime;  //时间差的毫秒数
    var strTimeShow;

    //计算出相差天数
    var days=Math.floor(date3/(24*3600*1000))
    //计算出小时数
    var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000))
    //计算相差分钟数
    var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000))
    //计算相差秒数
    var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000);


    if(hours == 0 && days == 0){
        if(0<minutes && minutes<10){
            strTimeShow = '刚刚';
        }else if(10<=minutes && minutes<60){
            strTimeShow = minutes +'分钟前';
        }
    }else if(hours != 0 && days == 0){
        if(1<=hours && hours<24){
            strTimeShow = hours +'小时前';
        }
    }else if(1<= days && days<365){
//		var arr = str.substring(0,10).split('-');
        strTimeShow = days +'天';
//		strTimeShow = arr[1] + '月' + arr[2] + '日';


        var arr = strTime.substring(0,10).split('-');
        strTimeShow = arr[1] + '月' + arr[2] + '日';
    }else{
        var arr = strTime.substring(0,10).split('-');
        strTimeShow = arr[0] + '年' + arr[1] + '月' + arr[2] + '日';
    }
    return strTimeShow;
}
function out(i,obj){
    var v = i.validity;
    if(true === v.valueMissing){
        i.setCustomValidity(obj.messing);
    }else{
        if(true === v.patternMismatch){
            i.setCustomValidity(obj.mismatch);
        }else{
            i.setCustomValidity("");
        }
    }
}











